package com.shangyueshang.controller.finance;

import com.shangyueshang.model.CompanyRevenue;
import com.shangyueshang.model.GoodsRevenue;
import com.shangyueshang.model.ShopRevenue;
import com.shangyueshang.request.AccountingDTO;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.GoodsRevenueService;
import com.shangyueshang.service.impl.RevenueService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/19 14:54
 * @version: v1.0.0
 */
@RestController
@RequestMapping("/revenue")
public class RevenueController {

    @Resource
    private RevenueService revenueService;

    @Resource
    private GoodsRevenueService goodsRevenueService;

    /**
     * 成本核算
     *
     * @return
     */
    @PostMapping("/execute")
    @PreAuthorize("hasAuthority('finance:revenue:execute')")
    public HttpResult<String> save(@Valid @RequestBody AccountingDTO accountingDTO) {
        return revenueService.save(accountingDTO);
    }

    /**
     * 从SKU维度查看营收
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/goods/findPage")
    @PreAuthorize("hasAuthority('finance:revenue:goods')")
    public HttpResult<PageResult<GoodsRevenue>> findPage(@RequestBody PageReq pageReq) {
        return HttpResult.ok(goodsRevenueService.findPage(pageReq));
    }

    /**
     * 从SKU维度导出营收数据
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/goods/export")
    @PreAuthorize("hasAuthority('finance:revenue:goods:export')")
    public ResponseEntity<org.springframework.core.io.Resource> goodsRevenueExport(@RequestBody PageReq pageReq) {
       return goodsRevenueService.goodsRevenueExport(pageReq);
    }


    /**
     * 店铺维度查看营收
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/shop/findPage")
    @PreAuthorize("hasAuthority('finance:revenue:shop')")
    public HttpResult<PageResult<ShopRevenue>> shopFindPage(@RequestBody PageReq pageReq) {
        return HttpResult.ok(revenueService.shopRevenueFindPage(pageReq));
    }

    /**
     * 公司维度查看营收
     *
     * @param pageReq
     * @return
     */
    @PostMapping("/company/findPage")
    @PreAuthorize("hasAuthority('finance:revenue:company')")
    public HttpResult<PageResult<CompanyRevenue>> companyFindPage(@RequestBody PageReq pageReq) {
        return HttpResult.ok(revenueService.companyRevenueFindPage(pageReq));
    }
}
